package com.atguigu.zqkutils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.atguigu.utils.JDBCUtils;
import com.sun.corba.se.spi.orbutil.fsm.Input;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Date:2020/8/28
 * Author:ybc
 * Description:
 */
public class ZqkJDBCUtils {
    private static DataSource dataSource;

    static {
        try {
        Properties properties = new Properties();

        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");


            properties.load(is);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  connection;
    }

    /**
     * 释放连接，不是关闭放弃，而是将连接还给数据库连接池
     *
     * @param connection
     */
    public static void closeConnection(Connection connection) {

        if (connection !=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
